const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');
//获取应用实例
const app = getApp()
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    banner: [],
    channel: [],
    video: [],
    image: [],
    logo: '/static/settings/logo.png',
  },

  attached() {
    this.getIndexData();
  },
  methods: {
    getIndexData: function () {

      let that = this;
      var data = new Object();
      //轮播图
      util.request(api.BANNER_LIST).then(function (res) {
        if (res.success === true) {
          data.banner = res.result;
          that.setData(data);
        }
      });
      //导航菜单
      util.request(api.MENU_LIST).then(function (res) {
        if (res.success === true) {
          data.channel = res.result;
          that.setData(data);
        }
      });
      //投票活动
      util.request(api.VOTE_LIST, {pageNumber: 1,pageSize: 1,sort: 'timeEnd', order: 'desc',}).then(function (res) {
        if (res.success === true) {
          let data = new Object();
          data.vote = res.result.content;
          that.setData(data);
        }
      });
      //视频回顾
      util.request(api.VIDEO_LIST, { pageNumber: 1, pageSize: 5,pushHome:1, sort: '[{field:"pushHome", order: "desc"},{field:"sortOrder", order: "asc"}]' }).then(function (res) {
        if (res.success === true) {
          ;
          data.video = res.result.content;
          that.setData(data);
        }
      });

      //精彩图集
      util.request(api.IMAGE_LIST, { pageNumber: 1, pageSize: 6, sort: "sortOrder", order: "asc" }).then(function (res) {
        if (res.success === true) {
          data.image = res.result.content;
          that.setData(data);
          that.towerSwiper();
        }
      });

    },
    //banner跳转其他页面
    bannerJump(e) {
      let endpoint = e.currentTarget.dataset.endpoint;
      let url = e.currentTarget.dataset.url;
      let page = endpoint === 'miniprogram' ? url : `/pages/webview/index?url=${endpoint}${url}`;
      wx.navigateTo({
        url: page,
      })
    },
    //video跳转播放页面
    videoJump(e) {
      let src = e.currentTarget.dataset.src;
      let poster = e.currentTarget.dataset.poster;
      let title = e.currentTarget.dataset.title;
      wx.navigateTo({
        url: `/pages/video/video-play/index?src=${src}&poster=${poster}&title=${title}`
      })
    },
    //投票明细页面
    goVoteItem(e) {
      let voteId = e.currentTarget.dataset.id;
      let voteName = e.currentTarget.dataset.votename;
      let voteDate = e.currentTarget.dataset.votedate;
      wx.navigateTo({
        url: `/pages/vote/vote-item-list/index?voteId=${voteId}&voteName=${voteName}&voteDate=${voteDate}`,
      });
    },
    // towerSwiper
    // 初始化towerSwiper
    towerSwiper() {
      let list = this.data.image;
      for (let i = 0; i < list.length; i++) {
        list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
        list[i].mLeft = i - parseInt(list.length / 2)
      }
      this.setData({
        image: list
      })
    },
    // towerSwiper触摸开始
    towerStart(e) {
      this.setData({
        towerStart: e.touches[0].pageX
      })
    },
    // towerSwiper计算方向
    towerMove(e) {
      this.setData({
        direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
      })
    },
    // towerSwiper计算滚动
    towerEnd(e) {
      let field = e.currentTarget.dataset.field;
      let direction = this.data.direction;
      let list = this.data.image;
      if (direction == 'right') {
        let mLeft = list[0].mLeft;
        let zIndex = list[0].zIndex;
        for (let i = 1; i < list.length; i++) {
          list[i - 1].mLeft = list[i].mLeft
          list[i - 1].zIndex = list[i].zIndex
        }
        list[list.length - 1].mLeft = mLeft;
        list[list.length - 1].zIndex = zIndex;
        this.setData({
          image: list
        })
      } else {
        let mLeft = list[list.length - 1].mLeft;
        let zIndex = list[list.length - 1].zIndex;
        for (let i = list.length - 1; i > 0; i--) {
          list[i].mLeft = list[i - 1].mLeft
          list[i].zIndex = list[i - 1].zIndex
        }
        list[0].mLeft = mLeft;
        list[0].zIndex = zIndex;
        this.setData({
          image: list
        })
      }
    }
  }
})